package com.mjf.cjt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.mjf.cjt.domain.TmProjectDicEntity;
import com.mjf.cjt.mapper.TmProjectDicMapper;
import com.mjf.cjt.service.AsyncTMService;
import com.mjf.cjt.service.TmProjectDicService;
import com.mjf.cjt.vo.ProjectResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/4/13 20:00
 */
@Service
public class TmProjectDicServiceImpl extends ServiceImpl<TmProjectDicMapper, TmProjectDicEntity> implements TmProjectDicService {

    @Autowired
    private AsyncTMService asyncTMService;

    @Override
    @Transactional
    public void syncProject() {
        try {
            List<ProjectResultVO> vos = asyncTMService.syncProject();
            if (CollectionUtil.isNotEmpty(vos)) {
                List<TmProjectDicEntity> entities = new ArrayList<>();
                for (ProjectResultVO row : vos) {
                    TmProjectDicEntity entity = new TmProjectDicEntity();
                    BeanUtil.copyProperties(row, entity);
                    entities.add(entity);
                }
                saveOrUpdateBatch(entities);
            }
        } catch (ChanjetApiException e) {
            throw new RuntimeException(e);
        }
    }
}
